package net.huashitong.zhApp.service;

import com.sirdc.modules.utils.NumberUtils;
import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.Project.entity.NewProjects;
import net.huashitong.oversee.dao.GradePointDao;
import net.huashitong.oversee.databean.ProjectScoreRankDataBean;
import net.huashitong.oversee.entity.ScoreSummary;
import net.huashitong.oversee.filter.ScoreSummaryFilter;
import net.huashitong.oversee.service.GradePointService;
import net.huashitong.oversee.service.ScoreSummaryService;
import net.huashitong.user.entity.BnpUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AppGradePointService {

    @Autowired
    private ScoreSummaryService scoreSummaryService;
    @Autowired
    private GradePointDao gradePointDao;

    public Map<String, String> qureyScoreAndRankByBnpUser(Map<String, String> jobGroupMap, BnpUser bnpUser) {

        Map<String, String> resultMap = new HashMap<>();
        String checkObjectId = "";
        String branchType = "——";
        if (!"2".equals(bnpUser.getBranchType()) && !"4".equals(bnpUser.getBranchType())) {
            checkObjectId = bnpUser.getSysId();
            if ("1".equals(bnpUser.getBranchType())) {
                branchType = "审批部门";
            }
            if ("0".equals(bnpUser.getBranchType())) {
                branchType = "业主单位";
            }
        }
        //督查组
        if ("4".equals(bnpUser.getBranchType())) {
            if (StringUtils.isNotBlank(jobGroupMap.get(bnpUser.getPhone()))) {
                checkObjectId = jobGroupMap.get(bnpUser.getPhone());
            }
            branchType = "督查组";
        }
        //单独出来的一个汇总信息
        //非督查组
//        DecimalFormat df = new DecimalFormat("#.00");
        if (!"2".equals(bnpUser.getBranchType())) {
            ScoreSummaryFilter scoreSummaryFilter = new ScoreSummaryFilter();
            scoreSummaryFilter.setUserId(checkObjectId);
            List<ScoreSummary> scoreSummaries = scoreSummaryService.queryByFilterNoPage(scoreSummaryFilter);
            if (ObjectUtils.isNotBlank(scoreSummaries)) {
                ScoreSummary summary = scoreSummaries.get(0);
                resultMap.put("finalScore", StringUtils.isBlank(summary.getScore()) ? "——" : NumberUtils.keepPrecision(summary.getScore(), 2) + "");   //最终得分
                resultMap.put("finalRank", StringUtils.isBlank(summary.getRank()) ? "——" : summary.getRank());     //最终排名
                resultMap.put("itemRank", StringUtils.isBlank(summary.getTypeRank()) ? "——" : summary.getTypeRank());  //组内排名
                resultMap.put("name", StringUtils.isBlank(bnpUser.getName()) ? "——" : bnpUser.getName());          //名字
                resultMap.put("branch", StringUtils.isBlank(bnpUser.getBranch()) ? "——" : bnpUser.getBranch());      //组别
                resultMap.put("branchType", branchType);      //组别
            } else {
                resultMap.put("finalScore", "任务尚未完成，故无最终得分");   //最终得分
                resultMap.put("finalRank", "——");     //最终排名
                resultMap.put("itemRank", "——");  //组内排名
                resultMap.put("name", StringUtils.isBlank(bnpUser.getName()) ? "——" : bnpUser.getName());          //名字
                resultMap.put("branch", StringUtils.isBlank(bnpUser.getBranch()) ? "——" : bnpUser.getBranch());
                resultMap.put("branchType", branchType);      //组别
            }
        }
        return resultMap;
    }


    //统计项目得分(分管领导)
    public List<ProjectScoreRankDataBean> statisticalItemScore(String resPhone, String status) {
        List<ProjectScoreRankDataBean> result = gradePointDao.statisticalItemScore(resPhone, status);
        for (ProjectScoreRankDataBean dataBean : result) {
            Number count = dataBean.getCount();
            int countInt = count.intValue();
            Double avgScore = dataBean.getScore();
            Double finalScore = avgScore + avgScore * countInt / 10;
            dataBean.setFinalScore(finalScore);
        }
        return result;
    }


}
